package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
        implements AdChannelService {

    /**
     * 根据名称分页查询频道列表
     * 业务实现
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1.校检参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        //1.1校检分页查询参数
        dto.checkParam();
        //2.封装条件。执行查询
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        //2.1频道名称
        if (StringUtils.isNotBlank(dto.getName())){ //如果名称不为空为true
            queryWrapper.like(AdChannel::getName, dto.getName());  //方法参数(数据库字段，值)
        }
        //2.2状态
        if (dto.getStatus() != null){
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        //2.3序号升序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        //2.4分页
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.5执行查询
        IPage<AdChannel> pageResult = page(pageReq, queryWrapper);
        //3.封装响应结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(),
                pageResult.getRecords());
    }

    /**
     * 新增频道
     * 业务实现
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.参数校验
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())){
            //如果对象为空或频道名称为空则直接响应错误请求结果
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if(adChannel.getName() == null || adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无效参数！参数不能为空且长度不能大于10字符");
        }
        //2.判断该频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在，无法新增！");
        }
        //3.执行新增方法
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //4.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道信息
     * 业务实现
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        if (adChannel == null || adChannel.getId() == null){
            //判断频道参数或频道id为空
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        //2.根据id获取频道对象信息
        AdChannel channel = this.getById(adChannel.getId());
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        //3.校验名称唯一性 合法性
        if (StringUtils.isNotBlank(adChannel.getName()) && !adChannel.getName().equals(channel.getName())){
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count > 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道名称已存在");
            }
        }
        if(adChannel.getName() == null || adChannel.getName().length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无效参数！参数不能为空且长度不能大于10字符");
        }
        //4.执行修改
        this.updateById(adChannel);
        //5.返回结果
        return ResponseResult.okResult();
    }

    /**
     * 删除频道
     * 业务实现
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        /*//异常模拟
        if(true){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }*/
        //1.校验参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //2.判断当前id频道是否存在和有效
        AdChannel adChannel = this.getById(id);
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        //3.启用状态下不可删除
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道正在启用无法删除");
        }
        //4.删除频道
        this.removeById(id);
        //5.响应结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
